#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <QSerialPort>
#include <QByteArray>
#include <QMutex>
#include <QElapsedTimer>
#include <QThread>
#include <QFile>
#include <QTimer>
#include <QSqlDatabase>
#include <QSqlQuery>

class worker : public QObject
{
    Q_OBJECT
public:
    explicit worker(QMutex *mutex, QObject *parent = nullptr);
    ~worker();
public slots:
    void sendData(const QByteArray &data);
    //& 声明引用！当函数参数使用引用时，传递的是实参本身，而非实参的副本。
    //const修饰引用 表示该引用是常量引用 函数内部不能修改所指向的对象的值
    void sendFile(const QString &filePath,int frameLen,int frameGap);
    void cancelFileTransfer();
    void recvData();
    void closePort();
    void openPort(const QString &portName,int baudRate,int dataBits,int stopBits,int parity);
    bool isPortOpen() const;

private slots:
    void sendNextFileFrame();

private:
    QSerialPort *m_serialPort;
    QMutex *m_mutex;
    QFile m_currentFile;
    int m_frameLen;
    int m_frameGap;
    QTimer *m_fileSendTimer;
    qint64 m_totalFileSize;
    qint64 m_bytesSent;

signals:
    void sendResultTO(const QByteArray &result);
    void sendBytesWritten(int bytesWritten);
    void dataReceived(const QByteArray &buffer);
    void portOpened(bool success,const QString &message);
    void errorOccurred(const QString &error);
    void fileProgress(int percent);
    void fileTransferFinished();
};


#endif // WORKER_H
